package com.woniu.platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.platform.entity.PlatformUser;
import com.woniu.platform.mapper.PlatformUserMapper;
import com.woniu.platform.service.PlatformUserService;
import com.woniu.util.utils.HttpResult;
import com.woniu.util.utils.RequestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

@Service
public class PlatformUserServiceImpl implements PlatformUserService {
    @Autowired(required = false)
    private PlatformUserMapper platformUserMapper;
    @Override
    public HttpResult getUsersInfoByAccount(String account) {
        PlatformUser user = platformUserMapper.getUserByUserName(account);
        if(user != null){
            return new HttpResult().ok(user);
        }else{
            return new HttpResult().fail();
        }
    }

    /**
     * 通过不同的条件分页查询用户
     * @param request
     * @return
     */
    @Override
    public HttpResult queryPlatformUserInfo(RequestResult<PlatformUser> request) {
        PageHelper.startPage(request.getPageNum(),request.getPageSize());
        List<PlatformUser> platformusers = platformUserMapper
                .queryPlatformUserInfo(request.getData().getUserName(),request.getData().getAccount(),
                        request.getData().getPhone(),request.getData().getState(),request.getData().getPlatformrole().getId());
        PageInfo pageInfo = new PageInfo(platformusers);
        return new HttpResult().ok(pageInfo);
    }

    /**
     * 修改用户信息
     * @param platformUser
     * @return
     */
    public HttpResult changeUserInfo(PlatformUser platformUser){
        platformUserMapper.updateById(platformUser);
        platformUserMapper.changePlatformUandR(platformUser.getId(),platformUser.getPlatformrole().getId());
        return new HttpResult().ok();
    }

    /**
     * 添加用户信息
     * @param platformUser
     * @return
     */
    @Override
    public HttpResult insertUserInfo(PlatformUser platformUser) {
        platformUserMapper.insert(platformUser);
        platformUserMapper.insertPlatformUandR(platformUser.getId(),platformUser.getPlatformrole().getId());
        return new HttpResult().ok();
    }


}
